﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Win.Model;

namespace Win.DAL
{
    public class DishDAL
    {
        private DbContext dbContext = new exp03dbEntities1();

        public List<DishInfo> GetDishInfos()
        {
            DbSet<DishInfo> dishInfos = dbContext.Set<DishInfo>();
            List<DishInfo> dishes = new List<DishInfo>(dishInfos);
            return dishes;
        }

        public DishInfo GetDishById(int id)
        {
            DishInfo dish = dbContext.Set<DishInfo>().Where(u => u.DId.Equals(id)).FirstOrDefault();
            return dish;
        }

        public DishInfo GetDishByName(string name)
        {
            DishInfo dish = dbContext.Set<DishInfo>().Where(u => u.DTitle.Equals(name)).FirstOrDefault();
            return dish;
        }

        public List<DishInfo> GetDishesByNT(string name,string type)
        {
            if (name.Equals("") && type.Equals("全部"))
            {
                DbSet<DishInfo> dishes = dbContext.Set<DishInfo>();
                List<DishInfo> disheinfo = new List<DishInfo>(dishes);
                return disheinfo;
            }
            else 
            {
                if (!name.Equals(""))
                {
                    List<DishInfo> disheinfo = dbContext.Set<DishInfo>().Where(u => u.DTitle.Equals(name)).ToList<DishInfo>();
                    return disheinfo;
                }
                else
                {
                    List<DishInfo> disheinfo = dbContext.Set<DishInfo>().Where(u => u.DType.Equals(type)).ToList<DishInfo>();
                    return disheinfo;
                }
            }
        }

        public void AddDish(DishInfo dish)
        {
            dbContext.Entry<DishInfo>(dish).State = EntityState.Added;
            dbContext.SaveChanges();
        }

        public void UpdateDish(DishInfo dish)
        {
            dbContext.Entry(dish).State = EntityState.Modified;
            dbContext.SaveChanges();
        }

        public void DelDish(int id)
        {
            DishInfo dish = GetDishById(id);
            dbContext.Entry<DishInfo>(dish).State = EntityState.Deleted;
            dbContext.SaveChanges();
        }
        
    
    }
}
